package com.gsi.webstore.platform.storefront.pipelet.account;

import java.util.Iterator;

import com.intershop.beehive.core.capi.naming.NamingMgr;
import com.intershop.beehive.core.capi.pipeline.Pipelet;
import com.intershop.beehive.core.capi.pipeline.PipelineDictionary;
import com.intershop.beehive.bts.capi.orderprocess.StaticAddress;
import com.intershop.beehive.bts.capi.orderprocess.fulfillment.Order;
import com.intershop.beehive.core.capi.pipeline.PipeletExecutionException;
import com.intershop.beehive.core.capi.profile.Address;
import com.intershop.beehive.core.capi.profile.Profile;
import com.intershop.beehive.core.capi.profile.ProfileMgr;

/**
 * 
 */
public class CopyOrderAddressesToProfile extends Pipelet
{

    /**
     * Constant used to access the pipeline dictionary with key 'Order'
     */
    public static final String DN_ORDER = "Order";
    /**
     * Constant used to access the pipeline dictionary with key 'Profile'
     */
    public static final String DN_PROFILE = "Profile";

    /**
     * The pipelet's execute method is called whenever the pipelets gets
     * executed in the context of a pipeline and a request. The pipeline
     * dictionary valid for the currently executing thread is provided as a
     * parameter.
     * 
     * @param dict
     *            The pipeline dictionary to be used.
     * @throws PipeletExecutionException
     *             Thrown in case of severe errors that make the pipelet execute
     *             impossible (e.g. missing required input data).
     */
    public int execute(PipelineDictionary dict) throws PipeletExecutionException {        
        Order order = (Order)dict.get(DN_ORDER);
        if (null == order)
        {
            throw new PipeletExecutionException("Mandatory input parameter 'Order' not available in pipeline dictionary.");
        }
        Profile profile = (Profile)dict.get(DN_PROFILE);
        if (null == profile)
        {
            throw new PipeletExecutionException("Mandatory input parameter 'Profile' not available in pipeline dictionary.");
        }

        ProfileMgr mgr = (ProfileMgr)NamingMgr.getInstance().lookupManager("ProfileMgr");
        for (Iterator addresses = profile.getAddressBook().createAddressIterator(); addresses.hasNext(); )
        {
            Address address = (Address)addresses.next();
            mgr.removeAddress(profile.getAddressBook(), address);
        }
        for (Iterator addresses = order.createStaticAddressesIterator(); addresses.hasNext(); )
        {
            StaticAddress address = (StaticAddress)addresses.next();
            if ("CheckoutAddress".equals(address.getAddressName()))
            {
                Address addressNew = mgr.createAddress(profile.getAddressBook(), address.getAddressName());
                mgr.copyIntoAddress(addressNew, address);
                if (null != order.getShipToAddress() &&
                                address.getUUID().equals(order.getShipToAddress().getUUID()))
                {
                    profile.getAddressBook().setDefaultShipToAddress(addressNew);
                }
                if (null != order.getInvoiceToAddress() &&
                                address.getUUID().equals(order.getInvoiceToAddress().getUUID()))
                {
                    profile.getAddressBook().setDefaultInvoiceToAddress(addressNew);
                }
            }
        }
        mgr.assureAddressBookConsistency(profile.getAddressBook());

        return PIPELET_NEXT;
    }
}